//
//  UserApi.swift
//  APP
//
//  Created by Rdxer on 2017/12/8.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import Foundation
import FastComponent
import Moya
import CryptoSwift
import Device

/// 用户相关的
public enum UserApi {
    /// 登陆
    case Login(
        userName:String,
        password:String,
        validateCode:String?)
    
    /// 获取用户信息 //
    case getStaffInfo(staffNumber:String,orgCode:String)
    /// 检查 App 版本
    case check() // aph2/app/check
    /// 反馈
    case feedback(staffNumber:String,content:String) // aph2/app/feedback
    case getValidateCode() // sms/getValidateCode  token userName
    
    
}

extension UserApi:TargetType{
   
    public var baseURL: URL {
        return try! ServerConfig.baseURLString.asURL()
    }
    
    public var path: String {
        switch self {
        case .Login: return "app/login"
        case .getStaffInfo: return "staff/getStaff"
        case .check: return "aph2/app/check"
        case .feedback: return "aph2/app/feedback"
        case .getValidateCode: return "sms/getValidateCode"
        }
    }
    
    public var method: Moya.Method {
        switch self {
        case .getValidateCode():
            return .get
        default:
            return .post
        }
    }
    
    public var sampleData: Data {
        return "".data(using: .utf8)!
    }
    
    public var task: Task {
        switch self {
        default:
                return Task.requestParameters(parameters: parameters, encoding: URLEncoding.default)
            }
    }
    
    public var headers: [String : String]? {
        return nil
    }
    
    public var parameters: [String: Any]{
        switch self {
            case .Login(let userName, let password,let validateCode):
                return [
                    "userName":userName,
                    "password":password.md5()
                ].appendToken
                    .appendIfNotNil(key: "validateCode", value: validateCode)
            case let .getStaffInfo(staffNumber,orgCode):
                return [
                    "staffNumber":staffNumber,
                    "orgCode":orgCode
                ].appendToken
            case .check():
                return [
                    "platform":"iOS",
                    "version":Bundle.main.infoDictionary?["CFBundleVersion"] ?? ""
                ].appendToken
        case .feedback(let staffNumber, let content):
            return [
                "content":content,
                "staffNumber":staffNumber,
                "platform":Device.deviceTypeShowString,
                "version":UIDevice.current.systemVersion
                ].appendToken
        case .getValidateCode:
            return [:] //.appendToken.appendIfNotNil(key: "userName", value: LoginData.current?.USERNAME)
        }
    }
    
}
